package com.secsys.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.StringTokenizer;

public class Pager {

	private long totalRecords; // 总记录
	private long pageSize; // 每页显示的行数
	private long currentPage; // 当前页号
	private long nextPage; // 下一页
	private long previousPage; // 上一页
	private long startNum;
	private long endNum;
	private long totalPages; // 总页数
	private long startRow; // 当前页在数据库中的起始行
	private long pStartRow; // 上页在数据库中的起始行
	private long nStartRow; // 下页在数据库中的起始行
	private boolean hasPreviousPage;// 是否有下一页
	private boolean hasNextPage;// 是否有上一页
	private boolean hasPrevious10Page;// 是否有下10页
	private boolean hasNext10Page;// 是否有上10页
	private String URI;// 截取URL的URI部分
	private String queryString;// 不含参数“currentPage”的参数
	private String URIqueryString;// 整合前两者

	private static Log log = LogFactory.getLog(Pager.class);

	public Pager() {
		this.hasPreviousPage = false;
		this.hasNextPage = false;
		this.hasPrevious10Page = false;
		this.hasNext10Page = false;
	}

	public Pager getPager(long totalRecords, String currentPage, String uri,
			String qryString) throws Exception {
		// 分页部分
		Pager p = new Pager(totalRecords, Constant.PAGER_SIZE);
		if (currentPage == null || currentPage.equals("")) {
			p.toPage(1);
		} else {
			p.toPage(Long.valueOf(currentPage));
		}
		long start = p.getStartRow();
		long size = p.getPageSize();
		p.setURIqueryString(uri, qryString, "currentPage");
		return p;
	}

	public Pager(long totalRecords, long pageSize) {
		this.totalRecords = totalRecords;
		this.pageSize = pageSize;
		this.currentPage = 1;
		this.nextPage = 2;
		this.startRow = 0;
		this.totalPages = totalRecords / this.pageSize;
		if (this.totalRecords % this.pageSize > 0) {
			this.totalPages++;
		}
	}

	/*
	 * 主要分页函数
	 */
	public void toPage(long currentPage) {
		if (currentPage > totalPages) {
			currentPage = totalPages;
		}
		if (currentPage <= 0) {
			currentPage = 1;
		}
		startNum = currentPage - 3;
		if (startNum <= 0) {
			startNum = 1;
		}
		endNum = currentPage + 5;
		if (endNum > totalPages) {
			endNum = totalPages;
		}
		if (currentPage <= 1) {
			if (this.totalPages > 1) {
				hasNextPage = true;
			} else
				hasNextPage = false;
			hasPreviousPage = false;
			this.currentPage = 1;
			nextPage = 2;
			nStartRow = pageSize;
			startRow = 0;
		} else if (currentPage >= totalPages) {
			hasNextPage = false;
			if (this.totalPages > 1) {
				hasPreviousPage = true;
			} else
				hasPreviousPage = false;
			this.currentPage = totalPages;
			previousPage = this.currentPage - 1;
			pStartRow = (totalPages - 2) * pageSize;
			startRow = (totalPages - 1) * pageSize;
		} else {
			hasNextPage = true;
			hasPreviousPage = true;
			this.currentPage = currentPage;
			previousPage = this.currentPage - 1;
			pStartRow = (this.currentPage - 2) * pageSize;
			nextPage = this.currentPage + 1;
			startRow = (currentPage - 1) * pageSize;
		}
	}

	public long getStartNum() {
		return this.startNum;
	}

	public void setStartNum(long startNum) {
		this.startNum = startNum;
	}

	public long getEndNum() {
		return this.endNum;
	}

	public void setEndNum(long endNum) {
		this.endNum = endNum;
	}

	public String getURI() {
		return this.URI;
	}

	public void setURI(String URI) {
		this.URI = URI;
	}

	public String getQueryString() {
		return this.queryString;
	}

	public void setQueryString(String queryString, String contain) {
		String result = "", temp = "";
		StringTokenizer st = new StringTokenizer(queryString, "&");
		while (st.hasMoreTokens()) {
			temp = st.nextToken();
			if (!temp.contains(contain)) {
				result += temp + "&";
			}
		}
		this.queryString = result.substring(0, result.length() - 1);
	}

	public String getURIqueryString() {
		return this.URIqueryString;
	}

	// 传入一个contain参数，把queryString中含contain部分删去
	public void setURIqueryString(String URI, String queryString, String contain) {
		String result = "", temp = "";
		StringTokenizer st = new StringTokenizer(queryString, "&");
		while (st.hasMoreTokens()) {
			temp = st.nextToken();
			if (!temp.contains(contain)) {
				result += temp + "&";
			}
		}
		this.URIqueryString = URI + "?"
				+ result.substring(0, result.length() - 1);
	}

	public long getStartRow() {
		return startRow;
	}

	public long getTotalPages() {
		return totalPages;
	}

	public long getCurrentPage() {
		return currentPage;
	}

	public long getPageSize() {
		return pageSize;
	}

	public void setStartRow(long startRow) {
		this.startRow = startRow;
	}

	public void setTotalPages(long totalPages) {
		this.totalPages = totalPages;
	}

	public void setCurrentPage(long currentPage) {
		this.currentPage = currentPage;
	}

	public void setNextPage(long nextPage) {
		this.nextPage = nextPage;
	}

	public long getNextPage() {
		return this.nextPage;
	}

	public void setPreviousPage(long previousPage) {
		this.previousPage = previousPage;
	}

	public long getPreviousPage() {
		return this.previousPage;
	}

	public void setPageSize(long pageSize) {
		this.pageSize = pageSize;
	}

	public boolean getHasNextPage() {
		return hasNextPage;
	}

	public void setHasNextPage(boolean hasNextPage) {
		this.hasNextPage = hasNextPage;
	}

	public boolean getHasPreviousPage() {
		return hasPreviousPage;
	}

	public void setHasPreviousPage(boolean hasPreviousPage) {
		this.hasPreviousPage = hasPreviousPage;
	}

	public boolean getHasNext10Page() {
		return hasNext10Page;
	}

	public void setHas10NextPage(boolean hasNext10Page) {
		this.hasNext10Page = hasNext10Page;
	}

	public boolean getHasPrevious10Page() {
		return hasPrevious10Page;
	}

	public void setHasPrevious10Page(boolean hasPrevious10Page) {
		this.hasPrevious10Page = hasPrevious10Page;
	}

	public void setpStratRow(long pStartRow) {
		this.pStartRow = pStartRow;
	}

	public long getpStartRow() {
		return pStartRow;
	}

	public void setTotalRecords(long totalRecords) {
		this.totalRecords = totalRecords;
	}

	public long getTotalRecords() {
		return totalRecords;
	}

	public void setnStartRow(long nStartRow) {
		this.nStartRow = nStartRow;
	}

	public long getnStartRow() {
		return nStartRow;
	}

}
